package com.ruoyi.platform.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.platform.domain.*;
import com.ruoyi.platform.mapper.*;
import com.ruoyi.platform.vo.hik.HkAlarmData;
import com.ruoyi.platform.vo.request.WarningReportRequest;
import com.ruoyi.platform.vo.response.WarningReportDetails;
import com.ruoyi.platform.vo.response.WarningReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.platform.service.IMWarningService;

/**
 * 【预警信息】Service业务层处理
 * 
 * @author dataControl
 * @date 2025-09-15
 */
@Service
public class MWarningServiceImpl implements IMWarningService 
{
    @Autowired
    private MWarningMapper mWarningMapper;
    @Autowired
    private MMonitoringEquipmentMapper equipmentMapper;
    @Autowired
    private MStoreLocationMapper locationMapper;
    @Autowired
    private MExecuteCommandMapper commandMapper;
    @Autowired
    private MExecuteCommandResultMapper commandResultMapper;

    @Override
    public int insertMWarning(MWarning mWarning) {
        return mWarningMapper.insertMWarning(mWarning);
    }

    /**
     * 查询预警详情
     * @param warningId 【预警信息】主键
     * @return
     */
    @Override
    public MWarning selectById(String warningId) {
        return mWarningMapper.selectMWarningByWarningId(warningId);
    }

    /**
     * 查询【预警报表详情信息】
     * 
     * @param warningId 【预警信息】主键
     * @return 【预警信息】
     */
    @Override
    public WarningReportDetails queryReportDetailsByWarningId(String warningId)
    {
        MWarning mWarning = mWarningMapper.selectMWarningByWarningId(warningId);
        WarningReportDetails details = new WarningReportDetails();
        BeanUtils.copyProperties(mWarning,details);

        if(Objects.nonNull(details.getCommandResultId())){
            details.setCommandResult(commandResultMapper.queryCommandResultDetailsByResultId(details.getCommandResultId()));
        }
        details.setEquipment(equipmentMapper.selectMMonitoringEquipmentByEquipmentId(mWarning.getEquipmentId()));
        details.setLocation(locationMapper.selectMStoreLocationByStoreLocationId(mWarning.getStoreLocationId()));
        return details;
    }

    /**
     * 查询【预警信息】列表
     * 
     * @param mWarning 【预警信息】
     * @return 【预警信息】
     */
    @Override
    public List<MWarning> selectMWarningList(MWarning mWarning)
    {
        return mWarningMapper.selectMWarningList(mWarning);
    }

    /**
     * 修改【预警信息】
     * 
     * @param mWarning 【预警信息】
     * @return 结果
     */
    @Override
    public int updateMWarning(MWarning mWarning)
    {
        return mWarningMapper.updateMWarning(mWarning);
    }

    /**
     * 批量删除【预警信息】
     * 
     * @param warningIds 需要删除的【预警信息】主键
     * @return 结果
     */
    @Override
    public int deleteMWarningByWarningIds(String[] warningIds)
    {
        return mWarningMapper.deleteMWarningByWarningIds(warningIds);
    }

    /**
     * 删除【预警信息】信息
     * 
     * @param warningId 【预警信息】主键
     * @return 结果
     */
    @Override
    public int deleteMWarningByWarningId(String warningId)
    {
        return mWarningMapper.deleteMWarningByWarningId(warningId);
    }

    /**
     * 查询【预警台账】列表
     *
     * @param  queryRequest【预警台账】请求
     * @return 预警台账
     */
    @Override
    public List<WarningReportVo> queryWarningReports(WarningReportRequest queryRequest) {
        return mWarningMapper.queryWarningReports(queryRequest);
    }
    
    @Override
    public boolean saveAlarmWarningInfo(HkAlarmData alarmData) {
        MWarning warning = new MWarning();
        MMonitoringEquipment mMonitoringEquipment = equipmentMapper.selectMMonitoringEquipmentByIpAddress(alarmData.getIpAddress());
        warning.setEquipmentId(mMonitoringEquipment.getEquipmentId());
        MStoreLocation mStoreLocation = locationMapper.queryByEquipAndPresetNo(mMonitoringEquipment.getEquipmentId().toString(), String.valueOf(alarmData.getPresetNo()));
        warning.setStoreLocationId(mStoreLocation.getStoreLocationId());
        warning.setStoreLocationCode(mStoreLocation.getStoreLocationCode());
        warning.setWarningType(3L);
        warning.setCreateDate(new Date());
        warning.setWarningTime(alarmData.getDateTime());
        warning.setWarningContent(JSON.toJSONString(alarmData));
        try {
            if(!Objects.isNull(alarmData.getTaskID()) && !"".equals(alarmData.getTaskID()))
                warning.setCommandResultId(Long.valueOf(alarmData.getTaskID()));
        }catch (Exception e){
            //todo 业务暂不清晰 不确定taskId是否是来自当前命令执行iD
            System.out.println("非纯数字字符串错误");
        }
        return this.insertMWarning(warning) > 0;
    }
}
